package com.lepu.app.fun.chat.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.Vibrator;

import com.app.application.MyApplication;

/**
 * Created by weichyang on 2015/6/1.
 */
public class SingManager {
    private static SingManager instance = null;
    private Context context = MyApplication.getInstance();
    private AudioManager audioManager;
    private Vibrator vibrator;
    private long lastTime;

    private SingManager() {
    }

    public static SingManager getInstance() {
        if (instance == null) {
            synchronized (SingManager.class) {
                if (instance == null) {
                    instance = new SingManager();
                }
            }
        }
        return instance;
    }

    public void preparResource() {
        if (audioManager == null)
            audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
        if (vibrator == null)
            vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void setVoiceReminder() {
        long startTime = System.currentTimeMillis();
        if (startTime - lastTime < 1000)
            return;

        int modeType = 2;
        try {
            modeType = audioManager.getRingerMode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (modeType == 2) { //普通模式
            MediaPlayer mp = new MediaPlayer();
            try {
                mp.setDataSource(MyApplication.getInstance().getApplicationContext(), RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                mp.prepare();
                mp.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (null != vibrator) {
            vibrator.vibrate(new long[]{100, 500}, -1);
        }
        lastTime = startTime;
    }
}